import O1HD from "../../../engines/gameEngine";
import { CsvFileKind } from "../../../engines/libs/CsvFileKind";
import data_control from "../../../engines/libs/data_control";
import CsvDataManage from "../../../engines/managers/CsvDataManage";
import level_PeopleArcheryUnitBase from "../base/level_PeopleArcheryUnitBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_peopleArcheryEnemy extends level_PeopleArcheryUnitBase {

    @property({type:cc.Label,tooltip:"血量文本"})
    lbHp:cc.Label = null;

    @property({type:cc.Sprite,tooltip:"角色图标"})
    roleSp:cc.Sprite = null;

    @property({type:cc.Sprite,tooltip:"影子图标"})
    shadowSp:cc.Sprite = null;

    @property({type:cc.Sprite,tooltip:"底座图标"})
    pedestalSp:cc.Sprite = null;

    @property({type:cc.SpriteFrame,tooltip:"小怪图片"})
    smallSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"大怪图片"})
    bigSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"底座图片"})
    pedestalSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"影子图片"})
    shadowSps:cc.SpriteFrame[] = [];

    @property({type:cc.Sprite,tooltip:"受伤图片"})
    hurtSp:cc.Sprite = null;

    @property({type:cc.SpriteFrame,tooltip:"受伤图片"})
    hurtSps:cc.SpriteFrame[] = [];

    @property({type:cc.Node,tooltip:"血量节点"})
    hpNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"受击节点"})
    hurtEffect:cc.Node = null;

    @property({type:cc.SpriteFrame,tooltip:"仙的受击"})
    xianSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"魔的受击"})
    moSps:cc.SpriteFrame[] = [];

    @property({type:cc.Node,tooltip:"boss的云"})
    bossCloud:cc.Node = null;

    @property({type:cc.Node,tooltip:"普通的云"})
    normalCloud:cc.Node = null;

    @property({type:cc.Node,tooltip:"boss背景"})
    bossBg:cc.Node = null;

    @property({type:cc.Node,tooltip:"boss星星"})
    bossStar:cc.Node = null;

    waveData:any = null;

    /**获取当前血量 */
    get hp() {return this._hp;}
    /**设置当前血量 */
    set hp(hp:number){
        hp = Math.floor(hp);
        this._hp = hp;
        this.lbHp.string = hp.toString();
    }

    lastWaveFlag:boolean = false;

    /**怪物类型 */
    enemyType:number = 0;

    /**绑定类型  0是大怪 1是中怪 2是小怪 */
    bindType(type:number,lastWaveFlag:boolean,waveData:any){
        this.enemyType = type;
        this.lastWaveFlag = lastWaveFlag;
        this.waveData = waveData;
        this.refrenshSize();
    }    

    refrenshSize(){
        switch(this.enemyType){
            case 0:{
                this.bossCloud.active = true;
                this.bossBg.active = true;
                this.bossStar.active = true;
                this.normalCloud.active = false;
                this.roleSp.spriteFrame = data_control.getRandItemOfArray(this.bigSps);
                this.pedestalSp.spriteFrame = this.pedestalSps[0];
                this.shadowSp.spriteFrame = this.shadowSps[0];
                let nCollider = this.node.getComponent(cc.BoxCollider);
                nCollider.size = cc.size(600,150);
                this.hpNode.y = -100;
                break;
            }
            default:{
                if(this.enemyType == 1){
                    this.pedestalSp.spriteFrame = this.pedestalSps[0];
                    let nCollider = this.node.getComponent(cc.BoxCollider);
                    nCollider.size = cc.size(600,150);
                    this.hpNode.y = -100;
                    this.bossCloud.active = true;
                    this.bossBg.active = true;
                    this.bossStar.active = true;
                    this.normalCloud.active = false;
                }else{
                    this.pedestalSp.spriteFrame = this.pedestalSps[1];
                    this.roleSp.node.scale = this.roleSp.node.scale * 0.8;
                    // this.pedestalSp.node.scale = this.pedestalSp.node.scale * 0.8;
                    this.hurtSp.node.scale = this.hurtSp.node.scale * 0.8;
                    this.bossCloud.active = false;
                    this.normalCloud.active = true;
                    this.bossStar.active = false;
                    this.bossBg.active = false;
                }
                this.shadowSp.spriteFrame = this.shadowSps[1];
                this.roleSp.spriteFrame = this.smallSps[this.enemyType - 1];
                this.hurtSp.spriteFrame =this.hurtSps[this.enemyType -1];
                this.hpNode.y = 20;
                break;
            }
        }
    }

    update(dt: number): void {
        super.update(dt);
        if(this.level.gamePauseFlag){
            return;
        }
        let moveDis = this.level.getRoleMoveSpd() * dt;
        this.node.y -= moveDis;
        this.autoDeath();
    }

    async unitBeHurt(atk: number,atkType?:string,criticalFlag?:boolean){
        if(!this.isDeath){
            this.level.damage += atk;
            this.beHurtEffect();
            return super.unitBeHurt(atk,atkType,criticalFlag);
        }
    }

    beHurtEffect(){
        let randX:number = data_control.getRandNumberOfArrayTwo([-100,100]);
        let randY:number = data_control.getRandNumberOfArrayTwo([0,200]);
        let node:cc.Node = cc.instantiate(this.hurtEffect);
        node.active = true;
        node.setParent(this.node);
        node.setPosition(randX,randY);
        let roleProfile:string = this.level.role.profile;
        let spAry:cc.SpriteFrame[] = [];
        switch(roleProfile){
            case "0":
            case "1":{
                spAry = this.xianSps
                break;
            }
            case "2":{
                spAry = this.moSps;
                break;
            }
        }
        let randSp = data_control.getRandItemOfArray(spAry);
        node.getComponent(cc.Sprite).spriteFrame = randSp;
        cc.tween(node).delay(0.2).to(0.5,{opacity:0}).call(()=>{
            node.destroy();
            node.removeFromParent();
        }).start();

    }

    async unitDeath(){
        switch(this.enemyType){
            case 0:{
                O1HD.getManagers().soundMgr.playSound("peopleArcherySound/Boss");
                let boxTypeId = this.waveData.boss_drop_type;
                let boxdata = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryBox,boxTypeId);
                let dropType:string[] = boxdata["drop_type"].split("&");
                // 需要打开界面
                let datas:any[] = [];
                for(let i = 0;i<dropType.length;i++){
                    let drop = dropType[i];
                    let data = this.level.getOptions(drop,1);
                    datas.push(data[0]);
                }
                // this.level.openBoxPage(datas,this.lastWaveFlag);
                if(this.lastWaveFlag){
                    // 打开获得修为界面
                    this.level.openMoneyPage();
                }else{
                    this.level.openRouGePage();
                    
                }
                // 如果角色需要回血 则计算角色需要的回血量
                let bossDeathRevitalizePercent:number = this.level.role.bossDeathRevitalizePercent;
                if( bossDeathRevitalizePercent > 0){
                    let maxHp:number = this.level.role.maxHp;
                    let addition:number = maxHp * bossDeathRevitalizePercent;
                    let data = {param_type:"0",option_param:addition,reCoverMaxHp:"0"};
                    this.level.parseDataAddPeople(data);
                }
                this.level.role.showTitle("2");
                break;
            }
            case 1:{
                this.level.openRouGePage();
                // 如果角色需要回血 则计算角色需要的回血量
                let bossDeathRevitalizePercent:number = this.level.role.bossDeathRevitalizePercent;
                if( bossDeathRevitalizePercent > 0){
                    let maxHp:number = this.level.role.maxHp;
                    let addition:number = maxHp * bossDeathRevitalizePercent;
                    let data = {param_type:"0",option_param:addition,reCoverMaxHp:"0"};
                    this.level.parseDataAddPeople(data);
                }
                break;
            }
            default:{
                this.level.createOneBox(this);
                break;
            }
        }
        this.level.killCount ++;
        this.level.killOneEnemy(this.enemyType);
        super.unitDeath();
    }

    
    async unitBeHurtAni(){
        const width = 4;
        cc.Tween.stopAllByTarget(this.roleSp.node);
        this.roleSp.node.x = 0;
        this.roleSp.node.y = 15;
        let once = cc.tween(this.roleSp.node)
        .by(0.02, { x: width, y: width })
        .by(0.02, { x: 0, y: -width })
        .by(0.02, { x: -width, y: -width })
        .by(0.02, { x: -width, y: 0 })
        .by(0.02, { x: 0, y: width })
        .by(0.02, { x: width, y: 0 });
        cc.tween(this.roleSp.node)
        .then(once)
        .start();
    }

    redSharkAni(){
        cc.Tween.stopAllByTarget(this.hurtSp.node);
        this.hurtSp.node.active = true;
        cc.tween(this.hurtSp.node)
        .delay(0.2)
        .call(()=>{
            this.hurtSp.node.active = false;
        })
        .start();
    }
}
